package com.ruifu.worksheet.feign;

import com.fhzncloud.cloud.common.security.annotation.Inner;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.feign.RemoteDocService;
import com.ruifu.common.po.DmDocVer;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.common.vo.worksheet.WsCommonSheet;
import com.ruifu.worksheet.service.WsCommonSheetService;
import com.ruifu.worksheet.service.WsCustomizeTypeService;
import com.ruifu.worksheet.util.EnumUtil;
import com.ruifu.worksheet.vo.WsCommonSheetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;

/**
 * 工作单feign层
 *
 * @author yuanyundi
 * @create 2020/05/15
 */

@RestController
@CrossOrigin
@RequestMapping("/feignWorkSheet")
public class FeignWorkSheetController {
    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    @Autowired
    WsCommonSheetService wsCommonSheetService;

    @Autowired
    WsCustomizeTypeService wsCustomizeTypeService;

    @Autowired
    private RemoteDocService remoteDocService;

    @Autowired(required = false)
    private DmDocVer dmDocVer;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 状态：1-处理中
     */
    private static final String KEY_DEAL = "1";

    /**
     * 状态 2-已关闭
     */
    private static final String KEY_END = "2";

    /**
     * 标识  作废通知单
     */
    private static final int FLAG_CANCEL_SHEET = 3;

    /**
     * 标识  变更通知单
     */
    private static final int FLAG_ECN_SHEET = 2;

    /**
     * 任务待办
     *
     * @param cmId 工作单ID
     * @param classFlag 工作单Feign表标识号 问题报告单:pes  作废通知单:vns 变更申请单:crs 变更通知单:cns
     * @return WsCommonSheetVO 工作单数据
     */
    @GetMapping("/feignFindWsById/{cmId}/{classFlag}")
    public WsCommonSheet feignFindWsById(@PathVariable("cmId") BigInteger cmId,
                                         @PathVariable("classFlag") String classFlag){
        WsCommonSheetVO wsCommonSheetVO =  wsCommonSheetService.findWorkSheetById(cmId,Integer.parseInt(EnumUtil.valueOf(classFlag).getClassName()),cloudUser);
        WsCommonSheet wsCommonSheet  = BeanConverter.copyProperties(wsCommonSheetVO,WsCommonSheet.class);
        //设置类别
        wsCommonSheet.setCmFeginClassFlag(classFlag);
        return wsCommonSheet;
    }

    /**
     * 流程审批操作
     *
     * @param cmId 工作单ID
     * @param classFlag 工作单Feign表标识号 问题报告单:pes  作废通知单:vns 变更申请单:crs 变更通知单:cns
     * @isEnd 是否最后一步流程节点操作 false:否 true:是
     */
    @GetMapping("/feignReviewCommit/{cmId}/{classFlag}/{isEnd}")
    public void feignReviewCommit(@PathVariable("cmId") BigInteger cmId,
                                  @PathVariable("classFlag") String classFlag,
                                  @PathVariable("isEnd") Boolean isEnd){
        //转义feignClassFlag
        int intClassFlag = Integer.parseInt(EnumUtil.valueOf(classFlag).getClassName());
        try {
            if(isEnd){
                //根据ID修改工作单状态,修改为2-已关闭
                wsCommonSheetService.updateStatusById(cmId,intClassFlag,KEY_END);
                //如果是作废通知单/变更通知单,需要调用文档feign接口
                if(intClassFlag == FLAG_CANCEL_SHEET || intClassFlag == FLAG_ECN_SHEET){
                    //根据ID查找问题对象关联ID集合
                    String relationIDS =  wsCommonSheetService.findAttAchFileIDS(cmId,intClassFlag);
                    if(relationIDS !=null && relationIDS.length() > 0){
                        String[] idArray = relationIDS.split(",");
                        for(String relationId:idArray){
                            //查询文档版本
                            dmDocVer = remoteDocService.feignFindByIdDoc(Long.valueOf(relationId));
                            //设置数据状态
                            if(intClassFlag == FLAG_CANCEL_SHEET){
                                dmDocVer.setDvCtrStatus(2);
                                dmDocVer.setDvCancelDate(new Date());
                            }else{
                                dmDocVer.setDvCtrStatus(1);
                                dmDocVer.setDvApprovedDate(new Date());
                            }
                            remoteDocService.feignUpdateDoc(dmDocVer);
                        }
                    }
                    //同步工作单附件修改问题对象的状态
                    wsCommonSheetService.updateAttachFileStatus(cmId,intClassFlag);
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 账套号数据同步
     *
     * @param sourceSetId  源
     * @param targetSetId  目标
     * @return boolean true:success,false:fail
     */
    @Inner
    @GetMapping(value = "/feignSynchroData/{sourceSetId}/{targetSetId}")
    public boolean feignSynchroData(@PathVariable("sourceSetId") Long sourceSetId,
                             @PathVariable("targetSetId") Long targetSetId){
        try {
            //copy 问题报告单
            wsCommonSheetService.copyWsCommonSheetList(sourceSetId,targetSetId,0);
            //copy 变更申请单
            wsCommonSheetService.copyWsCommonSheetList(sourceSetId,targetSetId,1);
            //copy 变更通知单
            wsCommonSheetService.copyWsCommonSheetList(sourceSetId,targetSetId,2);
            //copy 作废通知单
            wsCommonSheetService.copyWsCommonSheetList(sourceSetId,targetSetId,3);
            //copy 变更单类型
            wsCustomizeTypeService.copyWsCommonSheetList(sourceSetId,targetSetId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
